package com.runshu.newsmartark.util;

import android.text.TextUtils;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/11/9
 * @decs：
 */

public class UserCardUtil {
    /**
     * 逆序V卡卡号变正序(仅支持偶数位反转)
     *
     * @param cardId
     * @return
     */
    public static String reverseCardId(String cardId) {
        String[] arrs = new String[cardId.length()];
        for (int i = 0; i < cardId.length(); i++) {
            if (i == (cardId.length() - 1)) {
                arrs[i] = cardId.substring(i);
            } else {
                arrs[i] = cardId.substring(i, i + 1);
            }

        }
        for (int i = 0; i < arrs.length; i++) {
            if ((i % 2) == 1) {
                String temp = arrs[i];
                arrs[i] = arrs[i - 1];
                arrs[i - 1] = temp;
            }
        }
        StringBuffer newCardId = new StringBuffer();
        for (int i = arrs.length - 1; i >= 0; i--) {
            newCardId.append(arrs[i]);
        }
        return newCardId.toString();
    }

    /**
     * 十进制卡转16进制卡
     *
     * @param cardId
     * @return
     */
    public static String decimal2Hex(String cardId) {
        String hexCardId = Long.toHexString(Long.parseLong(cardId));
        if (hexCardId.length() < 8) {
            int spaceZeroCount = 8 - hexCardId.length();
            for (int i = 0; i < spaceZeroCount; i++) {
                hexCardId = "0" + hexCardId;
            }
        }else if(hexCardId.length()%2>0){
            hexCardId="0"+hexCardId;
        }
        return hexCardId.toUpperCase();
    }

    /**
     * 16进制卡转十进制卡
     * @param hex
     * @return
     */
    /*
     * 16进制字符串转字符串
     */
    public static String hex2String(String hex){
        if(hex == null || hex.length()==0){
            return hex;
        }
        return String.valueOf(Long.parseLong(hex, 16));
    }

}